package com.hrhr74.penguinstream.service.impl;

import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.hrhr74.penguinstream.constant.UserConstant;
import com.hrhr74.penguinstream.domain.dto.UserEditDTO;
import com.hrhr74.penguinstream.domain.po.User;
import com.hrhr74.penguinstream.service.UserEdit;
import com.hrhr74.penguinstream.utils.Result;
import org.springframework.stereotype.Service;

@Service(UserConstant.NORMAL_EDIT)
public class UserEditInfo  extends UserEdit {

    /**
     * 修改用户基本信息
     * @param userEditDTO
     * @return
     */

    @Override
    public Result editUser(UserEditDTO userEditDTO) {
        Long id = 1L;//TODO UserContext.getUser();
        if(userEditDTO == null){
            return Result.error("不支持当前修改方式！");
        }

        if(userEditDTO.getUsername() != null && Db.lambdaQuery(User.class).eq(User::getUsername, userEditDTO.getUsername()).exists()){
            return Result.error("当前用户已经存在！");
        }

        boolean update = Db.lambdaUpdate(User.class).eq(User::getId, id)
                .set(userEditDTO.getUsername() != null, User::getUsername, userEditDTO.getUsername())
                .set(userEditDTO.getEmail() != null, User::getEmail, userEditDTO.getEmail())
                .update();
        return update ? Result.ok("修改成功！") : Result.error("修改失败!");
    }
}
